package VectorApproach;

import java.util.ArrayList;
import java.util.List;

import Application.DirClassfication;
import Application.ImageGenerator;
import Application.ReadFileGenerateor;
import Application.TrajSplit;
import MyLoader.MyLoader;
import XmlLoader.FGObjProcess;
import XmlLoader.FGObjectTrajectoryStructure;
import XmlLoader.Position;

public class VMain {

	/**
	 * args[0] : file path of the trajectory index list
	 * args[1] : start index number of index content in args[0]
	 * args[2] : number of index content in args[0]
	 * args[3] : file path of directory storing list of trajectory xml file
	 * args[4] : the file path of storing cluster result
	 * args[5] : length of frame size
	 * args[6] : width of frame size
	 * args[7] : file path of background image
	 * args[8] : maximal distance of two vector
	 * args[9] : angle range per classification
	 * @param 
	 */
	public static void main(String[] args) {
		
		//generate list trajectory index
		System.out.println("Generate trajectory index file");
		new ReadFileGenerateor().ACT001_genFile(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2]));
								
		//load trajectory xml data
		System.out.println("Load trajectory xml file");
		FGObjProcess fgObj = new FGObjProcess(args[3]);
		List<String> list_objIndex = fgObj.ACT004_getStringList(args[0]);
		List<FGObjectTrajectoryStructure> listAll = new MyLoader().ACT001_getListOfObj(args[3], list_objIndex);	
		
		//trajectory segmentation
		System.out.println("Trajectory segmentation");
		TrajSplit split = new TrajSplit(Integer.parseInt(args[8]));
		ArrayList<TrajVector> listTrajV = new ArrayList<TrajVector>();
		FGObjectTrajectoryStructure traj = null;
		int index = 0;
		while(index < listAll.size()) {
			traj = listAll.get(index);
			if(traj.GET002_nNumberOfPoint() > 2) {
				split.readPosition(traj);
				split.compress(split.Positions.get(0), split.Positions.get(split.Positions.size() - 1));
				int indexResult = 0;
				ArrayList<TrajVector> listResult = split.transfromVector(60000);
				while(indexResult < listResult.size()) {
					listTrajV.add(listResult.get(indexResult));
					indexResult = indexResult + 1;
				}
			}
			index = index + 1;
		}
		while(listAll.size() > 0) {
			listAll.remove(0);
		}
		
		//ImageGenerator.GenTrajV(args[4] + "/" + index + ".png", listTrajV, Integer.parseInt(args[5]), Integer.parseInt(args[6]), args[7]);
		
		
		//direction classification
		ArrayList<ArrayList<TrajVector>> listDirCluster = DirClassfication.ACT002_dirVCluster(listTrajV, Integer.parseInt(args[9]));
		
		index = 0;
		while(index < listDirCluster.size()) {
			ArrayList<TrajVector> listOne = listDirCluster.get(index);
			if(listOne.size() > 0) {
				ImageGenerator.GenTrajV(args[4] + "/" + index + ".png", listTrajV, Integer.parseInt(args[5]), Integer.parseInt(args[6]), args[7], null);
			}
			index= index + 1;
		}
		//clustering
		index = 0;
		while(index < listDirCluster.size()) {
			VHierichicalCluster vhc = new VHierichicalCluster(10, Integer.parseInt(args[8]),
					new Position(0,0), new Position(Integer.parseInt(args[5]), Integer.parseInt(args[6])), args[7], args[4] + "\\" + index);
			vhc.ACT001_doCluster(listDirCluster.get(index));
			index = index + 1;
		}
	}
}
